<?php
class MemberAction extends BaseAction
{
    public function _initialize()
    {
        if (!isset($_SESSION['user_info']) && ACTION_NAME != 'verify' && ACTION_NAME != 'loginup' && ACTION_NAME != 'login' && ACTION_NAME != 'dologin' && ACTION_NAME != 'register' && ACTION_NAME != 'doregister') {
            redirect(u('Member/login'));
        }
    }

    public function Index()
    {
        $this->display();
    }

    public function myorder()
    {
    	$trade_mod = M('Trades');
    	$order_mod = M('Order');
    	$user_info = unserialize($_SESSION["user_info"]);
    	$uid = $user_info['uid'];
    	$trades = $trade_mod->where('uid='.$uid)->findAll();
    	$count = $trade_mod->where('uid='.$uid)->count();
    	foreach( $trades as $key=>$val){
    		$tid = $val['tid'];
    		$trades[$key]['orders'] = $order_mod->where("tid='".$tid."'")->select();
    	}
		//dump($trades);exit;
    	$this->assign('trades',$trades);
    	$this->assign('num',$count);
        $this->assign('my_left', 'myorder');
        $this->display();
    }

    public function profile()
    {
    	$member_mod = M('Member');
    	$user_info = unserialize($_SESSION["user_info"]);
    	$uid = $user_info['uid'];
    	$user = $member_mod->where('uid='.$uid)->find();
    	$this->assign('user',$user);
        $this->assign('my_left', 'profile');
        $this->display();
    }

    public function edit1()
    {
    	$member_mod = M('Member');
	    if($_POST['dosubmit']){
	    	$data = $member_mod->create();
	    	//dump($data);
	    	$result = $member_mod->save();
	    	if(false !== $result){
	    		$this->success('修改成功');
	    	}else{
	    		$this->error('修改失败');
	    	}
    	}
    }

    public function edit2()
    {
    	$member_mod = M('Member');
    	if($_POST['dosubmit']){
    		$data['uid'] = $uid = isset($_POST['uid']) && intval($_POST['uid']) ? intval($_POST['uid']):$this->error('参数错误');
    		$data['password'] = $password = isset($_POST['password']) && trim($_POST['password']) ? md5(trim($_POST['password'])) : $this->error('请输入当前密码');
    		$newpassword = isset($_POST['newpass']) && trim($_POST['newpass']) ? trim($_POST['newpass']):$this->error('请输入新密码');
			if($newpassword === $_POST['repass']){
				$user = $member_mod->field('password')->where('uid='.$uid)->find();
				if($password !== $user['password']){
					$this->error('当前密码错误');
				}
				$result = $member_mod->save($data);
				if(false !== $result){
		    		$this->success('修改成功');
		    	}else{
		    		$this->error('修改失败');
		    	}
			}else{
				$this->error('重复密码错误');
			}
    	}
    }

    public function address()
    {
        //获取用户所有收获地址
        $address_mod = D('Address');
        $user_info = unserialize($_SESSION["user_info"]);
        $address_list = $address_mod->where('uid='.$user_info['uid'])->findAll();
        $address_default = array();
        foreach ($address_list as $val) {
            if ($val['is_default']) {
                $address_default = $val;
            }
        }
        $this->assign('address_default', $address_default);
        $this->assign('address_list', $address_list);
        $this->assign('my_left', 'address');
        $this->display();
    }

    public function add_address()
    {
        $address_mod = D('Address');
        $user_info = unserialize($_SESSION["user_info"]);
        $_POST['uid'] = $user_info['uid'];
        if (empty($_POST['consignee'])) {
            $this->error('请填写收货人姓名！');
        }
        if (empty($_POST['province'])) {
            $this->error('请选择省份！');
        }
        if (empty($_POST['city'])) {
            $this->error('请选择省份！');
        }
        if (empty($_POST['area'])) {
            $this->error('请选择城市！');
        }
        if (empty($_POST['address'])) {
            $this->error('请填写详细地址！');
        }
        if (empty($_POST['zipcode'])) {
            $this->error('请填写邮政编码！');
        }
        if (empty($_POST['mobile'])) {
            $this->error('请填写手机号码！');
        }

        if( false === $vo = $address_mod->create() ){
	        $this->error( $address_mod->error() );
	    }
	    $address_mod->add();
        $this->success("添加成功！");
    }

    public function register()
    {
        $this->display();
    }

    public function doregister()
    {
        if(md5($_POST['verify']) != $_SESSION['verify']) {
            $this->error('验证码错误！');
        }
        if (!$_POST['agree']) {
            $this->error('请先阅读并同意注册协议！');
        }
        if (empty($_POST['email']) || empty($_POST['nickname'])) {
            $this->error('邮箱或昵称不能为空！');
        }
        $pattern = "/[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/i";
        $test = preg_match($pattern,$_POST['email']);
        if(0 === $test){
            $this->error('邮箱格式不合法！');
        }
        if (empty($_POST['password'])) {
            $this->error('请填写密码！');
        }
        if ($_POST['password']!=$_POST['repassword']) {
            $this->error('两次填写密码不一致！');
        }
        // 检查是否已经被注册
        $member_mod = D('Member');
        $is_regiter = $member_mod->where("email='".$_POST['email']."'")->count();
        if ($is_regiter) {
            $this->error('此邮箱已经被注册！');
        }

        $data['email'] = trim($_POST['email']);
        $data['nickname'] = trim($_POST["nickname"]);
        $data['password'] = md5($_POST["password"]);
        $data['is_subscribe'] = $_POST["is_subscribe"];
        $data['type'] = 'local';
        $data['reg_time'] = time();
        import("ORG.Net.IpLocation");
        $data['reg_ip'] = IpLocation::get_client_ip();
        $uid = $member_mod->add($data);

        //用户登陆
        $user_info['uid'] = $uid;
        $user_info['email'] = $_POST['email'];
        $user_info['nickname'] = $_POST['nickname'];
        $_SESSION['user_info'] = serialize($user_info);
        $this->success("注册成功！");
    }

    public function login()
    {
        if (isset($_SESSION['user_info'])) {
            redirect(u('Member/index'));
        }
        $this->display();
    }

    public function dologin()
    {
        $map["email"]  = $_POST["email"];
        $map["password"] = md5($_POST["password"]);
        $member_mod = D('Member');
        $user = $member_mod->where($map)->field("uid,email,nickname")->find();
        if ($user) {
            $user_info['uid'] = $user['uid'];
            $user_info['email'] = $_POST['email'];
            $user_info['nickname'] = $_POST['nickname'];
            $_SESSION['user_info'] = serialize($user_info);

            //更新登陆时间和IP
            import("ORG.Net.IpLocation");
            $update['last_ip'] = IpLocation::get_client_ip();
            $update['last_time'] = time();
            $member_mod->where('uid='.$user['uid'])->save();
            $this->success("登陆成功！", $this->get_loginpage(__APP__));
        } else {
            $this->error("帐号或密码不对！");
        }
    }

    public function get_loginpage($default = null)
    {
        if (isset($_SESSION['last_page'])) {
            return $_SESSION['last_page'];
        } else {
            return $default;
        }
    }

    public function loginup()
    {
        $this->display();
    }

    public function logout()
    {
        unset($_SESSION['user_info']);
        Session::clear();
        redirect(u('Member/login'));
    }

    public function verify()
    {
        $type  = isset($_GET['type'])?$_GET['type']:'gif';
        import("ORG.Util.Image");
        Image::buildImageVerify(4,1,$type);
    }
}
?>